import { CreepHelper } from "../../utils/creepHelper";

class BuildTask implements ICreepTask {
  public run(creep: Creep): void {
    if(creep.memory.working) {
      
      if(creep.memory.buildTarget){
        let target = Game.getObjectById(creep.memory.buildTarget.id);
        let deleteTarget = false;
        let result = 0;

        if(target){
          if(creep.memory.buildTarget.isConstructionSite){
            // 工地
            const site = target as ConstructionSite;
            result = creep.build(site);
            if(result === ERR_NOT_IN_RANGE){
              creep.go(site);
            }
          }
          else {
            // 建筑
            const structure = target as Structure;
            // 检查建筑的修复程度是否 >= 40%
            if((structure.hits === structure.hitsMax )){
              deleteTarget = true;
            }
            result = creep.repair(structure);
            if(result === ERR_NOT_IN_RANGE){
              creep.go(structure);
            }
          }
        }
        else {
          deleteTarget = true;
        }

        if(deleteTarget){
          if(creep.memory.outerRoom){
            // 如果是外矿房间需要把外矿房间的缓存进行删除
            const room = Game.rooms[creep.memory.outerRoom];
            if(room){
              if(creep.memory.buildTarget.isConstructionSite){
                room.memory.constructions.splice(room.memory.constructions.indexOf(creep.memory.buildTarget.id as Id<ConstructionSite>),1);
              }
              else {
                room.memory.repairs.splice(room.memory.repairs.indexOf(creep.memory.buildTarget.id as Id<Structure>),1);
              }
            }
          }
          delete creep.memory.buildTarget;
        }

        if(result === ERR_NOT_ENOUGH_RESOURCES){
          creep.memory.working = false;
        }
      }
      else {
        if(creep.memory.outerRoom){
          const room = Game.rooms[creep.memory.outerRoom];
          if(room){
            // 优先修复
            if(room.memory.repairs.length > 0){
              creep.memory.buildTarget = { id:room.memory.repairs[0], isConstructionSite:false };
            }
            else if(room.memory.constructions.length > 0){
              creep.memory.buildTarget = { id:room.memory.constructions[0], isConstructionSite:true };
            }
          }
        }
        else {
          // 获取距离最近的需要修复的建筑
          let repairTarget = this.findClosestStructureToRepair(creep);
          if(repairTarget){
            creep.memory.buildTarget = { id:repairTarget.id, isConstructionSite:false };
          }
          else{
            // 获取距离最近的未完成的建筑工地
            let buildTarget = this.findClosestConstructionSite(creep);
            if(buildTarget){
              creep.memory.buildTarget = { id:buildTarget.id, isConstructionSite:true };
            }
          }
        }
      }
    }
    else {
      //如果拾取未满最大容量则继续拾取
      CreepHelper.creepPickup(creep);
    }
  }

  private findClosestConstructionSite(creep: Creep): ConstructionSite | null {
    // 获取距离最近的未完成的建筑工地
    const sites = creep.room.find(FIND_MY_CONSTRUCTION_SITES);
    return sites.length > 0 ? creep.pos.findClosestByRange(sites) : null;
  }

  private findClosestStructureToRepair(creep: Creep): Structure | null {
    // 获取距离最近的需要修复的建筑
    const structures = creep.room.find(FIND_MY_STRUCTURES, {
      filter: (structure) => {
        return (structure.structureType !== STRUCTURE_RAMPART) &&
               (structure.hits < structure.hitsMax) &&
               (structure.hits / structure.hitsMax < 0.4);
      }
    });
    return structures.length > 0 ? creep.pos.findClosestByRange(structures) : null;
  }
}

export { BuildTask };